import {
  Entity,
  PrimaryGeneratedColumn,
  OneToMany,
  Column,
  ManyToOne,
  ManyToMany,
  TreeChildren,
  TreeParent,
  Tree,
} from 'typeorm';
import { AccessEntity } from '../access/access.entity';
import { UsersEntity } from '../users/users.entity';

@Entity({ name: 'department' })
@Tree('closure-table')
// @Tree("materialized-path")
// @Tree("nested-set")
export class DepartmentEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('varchar')
  name: string;

  @Column('varchar')
  phone: string;

  @Column('int')
  type: number;

  @Column('datetime')
  createDate: string;

  @OneToMany(
    () => UsersEntity,
    users => users.department,
    { cascade: true, eager: true },
  )
  users: UsersEntity[];

  @ManyToMany(
    type => UsersEntity,
    leaders => leaders.chargeDepartment,
    { cascade: true, eager: true },
  )
  leaders: UsersEntity[];

  @TreeChildren({ cascade: true })
  children: DepartmentEntity[];

  @TreeParent({ onDelete: 'CASCADE' })
  parent: DepartmentEntity;
}
